<!DOCTYPE html>
<html lang="en">
<title>新增活动</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">新增活动</h5>
                </div>
                <div class="ibox-content">
                    <form method="post" class="form-horizontal" action="activity_save" id="from" accept-charset="UTF-8">
                        <div class="form-group"><label class="col-sm-2 control-label">活动名称</label>
                            <div class="col-sm-10">
                                <input type="text" id="activityName" name="activityName" class="form-control" placeholder="必填，请输入活动名称">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">活动类型</label>
                            <div class="col-sm-10" id="activityType">
                                <div class="checkbox-inline i-checks"><label for="is_user"> <input type="radio" id="activityType0" value="0" checked name="activityType"> <i></i>订单成功后发起人获得优惠券</label></div>
                                <div class="checkbox-inline i-checks"><label for="is_system"> <input type="radio" id=activityType1 value="1" name="activityType"> <i></i>被邀请人点击后，邀请人获得优惠券</label></div>

                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                        	<input type="hidden" id="couponId" name ="couponId">
                            <label class="col-sm-2 control-label">优惠券ID</label>
                            <div class="col-sm-10" id="">
                                <input type="number" id="CouponIdChild" name="CouponIdChild" class="form-control" placeholder="必填，请输入优惠券ID">
                                <div id="add_list"></div>
                                <a href="javascript:;" class="pull-right" style="padding-top: 20px;color: #666;" id="add_input">再添加一条优惠券ID</a>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group" id="invitedUserId"><label class="col-sm-2 control-label">被邀请人券ID</label>
                            <input  type="hidden" id="inviteCouponId" name ="inviteCouponId">
                            <div class="col-sm-10">
                                <input type="number" id="inviteCouponIdChild"  class="form-control" placeholder="非必填，如有则填写，系统将结合带量帮您计算须返佣总数">
                                <div id="add_list1"></div>
                                <a href="javascript:;" class="pull-right" style="padding-top: 20px;color: #666;" id="add_input1">再添加一条被邀请人券ID</a>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">其他备注</label>
                            <div class="col-sm-10"><input type="text" id="remark" name="remark" class="form-control" placeholder="非必填"></div>
                        </div>
                        <div class="hr-line-dashed"></div>
                    </form>
                </div>
                <div class="ibox-content overflow">
                    <div class="col-sm-12">
                        <button type="button" class="btn btn-default pull-left" id="btn_cancel">取消添加</button>
                        <button type="button" class="btn btn-primary pull-right" id="btn_submit">确认添加</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<link href="${baseUrl}css/plugins/clockpicker/clockpicker.css" rel="stylesheet">
<script type="text/javascript" src="${baseUrl}js/main.js"></script>
<script src="${baseUrl}js/plugins/clockpicker/clockpicker.js"></script>
<script>
    $(document).ready(function () {
    	//radio 选择
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
        $('#add_input').click(function () {
            var list = '<div class="add-list overflow"><div class="col-sm-11" style="padding: 22px 15px 0 0;"><input type="number" class="form-control" placeholder="必填，请输入优惠券ID"></div><div class="col-sm-1" style="padding: 20px 15px 0 0;"><button type="button" class="btn btn-default btn-sm" onclick="delete_list(this)">删除</button></div></div>';
            $('#add_list').append(list);
        });
        $('#add_input1').click(function () {
            var list = '<div class="add-list overflow"><div class="col-sm-11" style="padding: 22px 15px 0 0;"><input type="number" class="form-control" placeholder="必填，请输入被邀请人券ID"></div><div class="col-sm-1" style="padding: 20px 15px 0 0;"><button type="button" class="btn btn-default btn-sm" onclick="delete_list(this)">删除</button></div></div>';
            $('#add_list1').append(list);
        });

    });
    function delete_list(obj) {
        $(obj).parents('.add-list').remove();
    }
    
    $("#btn_cancel").click(function(){
    	swal({
    		title: '取消保存？',
            text: "点击确定跳转查询列表页，点击刷新，刷新当前页面",
            type: 'question',
            showCancelButton: true,
            cancelButtonColor: '#d33',
            confirmButtonText: '确定',
            cancelButtonText: '刷新'
    		}).then(function () {
    			window.location.href = "to_activity_list";
    		}, function (dismiss) {
    		  if (dismiss === 'cancel') {
    			  history.go(0); 
    		  }
    		});
    });
    
    $("#btn_submit").click(function(){
    	var couponId = $("#CouponIdChild").val();
    	var inviteCouponId = $("#inviteCouponIdChild").val();
    	var msg = '';
    	
    	var activityName = $("#activityName").val();
    	if(activityName.length == 0){
    		msg = msg + '活动名称不能为空'+'</br>';
    	}
    	if(msg.length > 0){
    		swal({
                title: "提交错误",
                html: msg,
                type: "error"
            });
    		return false;
    	}
    	
		$("#add_list input[type='number']").each(function() {
				couponId = couponId +"#"+ $(this).val();
		});
		
		$("#add_list1 input[type='number']").each(function() {
			inviteCouponId = inviteCouponId + "#" + $(this).val();
		});
		$("#couponId").val(couponId);
		$("#inviteCouponId").val(inviteCouponId);
		
		 $.ajax({
             url: "activity_save",              // 请求地址
             data:$('#from').serialize(),// 你的formid,
             type: 'post',
             cache: false,
             dataType: 'json',
             success: function (data) {
            	 if(data.data =='0' || data.data == 0){
            		 swal("保存失败", "您的保存操作失败请联系管理员", "error");
            	 }else{
            		 /* sweetAlert("保存成功！", "正在为您跳转......", "warning"); */
            		  swal({
            	    		title: '保存成功！',
            	            text: "点击'确定'为您跳转查询列表，点击'继续'停留此页刷新当前页",
            	            type: 'question',
            	            showCancelButton: true,
            	            cancelButtonColor: '#d33',
            	            confirmButtonText: '确定',
            	            cancelButtonText: '继续'
            	    		}).then(function () {
            	    			window.location.href = "to_activity_list";
            	    		}, function (dismiss) {
            	    		  if (dismiss === 'cancel') {
            	    			  history.go(0); 
            	    		  }
            	    		});
            		 
            	 }
             },
             error: function () {
            	 swal("保存失败", "您的保存操作失败请联系管理员", "error")
             }
         });
		

	});
</script>
</@body.page>
</html>